#!/bin/bash
PROGRAM_NAME=$0
if [ "$1" == "--release" ]; then
	BUILD_DIR=build/release
	EXEC_BIN=bin/$PROGRAM_NAME
	CMAKE_BUILD_TYPE=Release
	shift 1
else
	BUILD_DIR=build/debug
	EXEC_BIN=bin/$PROGRAM_NAME-dbg
	CMAKE_BUILD_TYPE=Debug
fi
if [ ! -f "$BUILD_DIR/Makefile" ]; then
	echo "Build directory not yet initialised; running CMake..."
	mkdir -p "$BUILD_DIR"
	pushd "$BUILD_DIR"
	cmake -D "CMAKE_BUILD_TYPE:STRING=$CMAKE_BUILD_TYPE" "${DIRSTACK[1]}"
	popd
fi
make --no-print-directory -C "$BUILD_DIR" $PROGRAM_NAME
if [ $? -ne 0 ]; then exit $?; fi
exec $EXEC_BIN $*
